package com.mars.diancan.utils;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.util.Date;

/**
 * Date日期 转换为 Long
 * ---> 原有查询结果中Date属性的返回值是  1529916358000
 * ---> 与约定APi中的返回数据格式不符
 * ---> 修改如下:
 *    1.继承 JsonSerializer
 *    2.重写 serialize() 方法
 *
 * @author MARS
 * @date 2018/6/27
 */
public class Date2LongSerializer extends JsonSerializer<Date>{
    @Override
    public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
        jsonGenerator.writeNumber(date.getTime() / 1000);
    }
}
